#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
	ofSetVerticalSync(true);
    ofEnableSmoothing();
    ofEnableAlphaBlending();

	ofSetWindowTitle("VideoPile - Interactive Story Telling by Joey van der Bie");

	ofBackground(255,255,255);

	width = ofGetWidth();
	height = ofGetHeight();
	vloer.loadImage("vloer01.jpg");
	//selected = 0;

    myFont.loadFont("verdana.ttf", 12);

    init();
}

void testApp::init(){
    //movieArray[0].loadMovie("movies/fingers.mov");
	 movieArray[0].loadMovie("movies/0.mp4");
	movieArray[0].play();
	movieArray[0].animate(true);
	movieCount = 1;
	drawOrder[0] = 0;
}

//--------------------------------------------------------------
void testApp::update(){
    //update videoframes
    for(int i = 0; i< movieCount; i++){
        movieArray[i].idleMovie();
    }
}

//--------------------------------------------------------------
void testApp::draw(){

	ofSetColor(0xFFFFFF);
	vloer.draw(0,0);

    ofPushMatrix();
        ofTranslate(0, 85, 0);
        ofRotateZ(-6);

        ofSetColor(0xFFFFFFFF);
         myFont.drawString("VideoPile - Interactive Story Telling by Joey van der Bie (2010-06-28)",20,20);
         myFont.drawString("\"n\" for new movie",20,50);

         myFont.drawString("left mouse for duration",20,70);
         myFont.drawString("2x left mouse for move play/pause",20,90);

         myFont.drawString("right mouse for hold/move",20,110);
         myFont.drawString("2x right mouse for rotate/resize",20,130);
    ofPopMatrix();

    //Add new video if needed
    if(movieArray[movieCount-1].bShrinkEnd && movieCount< movieArraySize){
            addMovie();
    }
    //draw
    for(int i = 0; i< movieCount; i++){
            movieArray[drawOrder[i]].draw(width/2, height/2);
    }
}

void testApp::addMovie(){
    if(movieCount < movieArraySize){
            movieArray[movieCount-1].animate(true);
            movieArray[movieCount-1].setPaused(true);

            movieArray[movieCount].loadMovie("movies/"+ofToString(movieCount)+".mp4");
            movieArray[movieCount].play();
            movieArray[movieCount].animate(true);

            drawOrder[movieCount] = movieCount;

            movieCount++;
    }
}

void testApp::movieToTop(int index){
        for(int i=index; i< movieCount-1; i++){
            int top = drawOrder[i];
            drawOrder[i] = drawOrder[i+1];
            drawOrder[i+1] = top;
        }
}

//--------------------------------------------------------------
void testApp::keyPressed  (int key){
    switch(key){
        case 'n':
           addMovie();
        break;
        case 'f':
            ofToggleFullscreen();
        break;
    }
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
    for(int i=movieCount-1; i>=0 ; i--){
         if(movieArray[drawOrder[i]].mouseDragged(x,y,button)){
                i= -1;
         }
    }
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
    for(int i=movieCount-1; i>=0 ; i--){
         if(movieArray[drawOrder[i]].mousePressed(x,y,button)){
             movieToTop(i);
                i= -1;
         }
    }
}


//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
    for(int i=movieCount-1; i>=0 ; i--){
         if(movieArray[drawOrder[i]].mouseReleased(x,y,button)){
                i= -1;
         }
    }
}

//--------------------------------------------------------------
void testApp::resized(int w, int h){

}
